package com.sanpu.kxe.business.main.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.NewsContentOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;

import java.util.ArrayList;

import butterknife.ButterKnife;

/**
 * Description：新闻内容适配器
 * Created by 薛勇军 on 2018/8/29.
 */

public class NewsContentAdapter extends BaseMultiItemQuickAdapter<NewsContentOutput, BaseViewHolder> {

    private int mViewGroupWidth;

    public NewsContentAdapter(int mViewGroupWidth) {
        super(new ArrayList<NewsContentOutput>());
        addItemType(MainConfig.NEWS_THREE_IMAGE, R.layout.item_news_content_three);
        addItemType(MainConfig.NEWS_ONE_IMAGE, R.layout.item_news_content_one);
        addItemType(MainConfig.NEWS_ZERO_IAMGE, R.layout.item_news_content_zero);
        this.mViewGroupWidth = mViewGroupWidth;
    }

    @Override
    protected void convert(BaseViewHolder helper, NewsContentOutput item) {
        if (item != null) {
            helper.setText(R.id.titleTv, TextUtils.isEmpty(item.getNewsTitle()) ? "" : item.getNewsTitle())
                    .setText(R.id.sourceTv, TextUtils.isEmpty(item.getNewsSource()) ? "" : item.getNewsSource())
                    .setText(R.id.timeTv, TextUtils.isEmpty(item.getReleaseTime()) ? "" : item.getReleaseTime());
            switch (helper.getItemViewType()) {
                case MainConfig.NEWS_THREE_IMAGE:
                    String[] pictures = item.getPicturePath().split(",");
                    if (pictures.length > 0) {
                        LinearLayout newsImageLl = helper.getView(R.id.newsImageLl);
                        newsImageLl.removeAllViews();
                        for (int i = 0; i < pictures.length; i++) {
                            newsImageLl.addView(createMenuItem(newsImageLl, pictures[i]));
                        }
                    }
                    break;
                case MainConfig.NEWS_ONE_IMAGE:
                    ImageView newsImage = helper.getView(R.id.newsImage);
                    JKImageLoader.getInstance().displayImage(mContext,
                            UrlConstants.getBaseUrl()+item.getPicturePath(), newsImage);
                    break;
                case MainConfig.NEWS_ZERO_IAMGE:
                    break;
            }
        }
    }

    /**
     * Create Menu Item View
     * @return View
     */
    private View createMenuItem(ViewGroup viewGroup, String m) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.view_material_image, viewGroup, false);
        //用来显示按钮名称
        if (m != null) {
            ImageView materialImg = ButterKnife.findById(itemView, R.id.materialImg);
            if (mViewGroupWidth > 0) {
                resetViewSize(materialImg);
            }
            JKImageLoader.getInstance().displayImage(mContext, UrlConstants.getBaseUrl()+m, materialImg);
        }
        return itemView;
    }

    /**
     * 重置View大小
     *
     * @param iv target ImageView
     */
    private void resetViewSize(ImageView iv) {
        ViewGroup.LayoutParams layoutParams = iv.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = mViewGroupWidth;
            layoutParams.height = mViewGroupWidth*65/97;
        } else {
            layoutParams = new ViewGroup.LayoutParams(mViewGroupWidth, mViewGroupWidth*65/97);
        }
        iv.setLayoutParams(layoutParams);
    }
}
